﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using LuckyBingoRcon.Commands.Events;

namespace LuckyBingoRcon.Commands
{
	public class MapnameCommand : Command
	{
		//private List<EventHandler<MapnameCommandEventArgs>> handlers = new List<EventHandler<MapnameCommandEventArgs>>();
        public EventHandler<MapnameCommandEventArgs> OnResponse;

		public MapnameCommand()
        {
            base.commandText = "mapname";
            base.args = null;
        }

		//public void AttachHandler(EventHandler<MapnameCommandEventArgs> handler)
		//{
		//    handlers.Add(handler);
		//    OnResponse += handler;
		//}

		//public void DetachHandlers()
		//{
		//    foreach (EventHandler<MapnameCommandEventArgs> handler in handlers)
		//    {
		//        OnResponse -= handler;
		//    }

		//    handlers.Clear();
		//}

		protected override void CommandOnResponse(string response)
        {
            if (OnResponse != null)
            {
                string mapName = null;
                MapnameCommandEventArgs e = new MapnameCommandEventArgs();
                string s = base.Encoding.GetString(bytes, 5, bytes.Length - 5).Trim();
                base.sr = new StringReader(s);
                string ln = null;
                int index;
                try
                {
                    while (base.sr.Peek() > 0)
                    {
                        ln = base.sr.ReadLine().Trim();
                        if (!String.IsNullOrEmpty(ln) && (ln.StartsWith("\"mapname") || ln.StartsWith("mapname") || ln.StartsWith("\0ap")))            // Heli: Map name line has "mapname", not mapname at beginning
                        {
                            index = ln.IndexOf("mp_");                                      // Heli: Find (if packet was received correctly) the map name start index
                            if (index > 0)
                            {
                                ln = ln.Remove(0, index);                                       // Remove all characters before mp_ ...
                                index = ln.IndexOf("^7");
                                if (index > 0)
                                    ln = ln.Remove(index);
                                ln = ln.Trim();
                                if (new MapLookup().IsValidName(ln))
                                    mapName = ln;
                                break;
                            }
                        }
                    }

                    //MapnameCommandEventArgs e = new MapnameCommandEventArgs();
                    e.MapName = mapName;
                    OnResponse(this, e);
                }
                catch (Exception error)
                {
                    // some other error probably not related to the network
                    //MapnameCommandEventArgs e = new MapnameCommandEventArgs();
                    e.MapName = mapName;
                    e.Error = error;
                    OnResponse(this, e);
                }
                finally
                {
                    e = null;
                    mapName = null;
                    s = null;
                    base.sr = null;
                    ln = null;
                    bytes = null;
                }
            }
        }

        public override string ToString()
        {
            return "Get\tMap Name";
        }
	}
}
